﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PepsiCo.Business;

namespace PepsiCo.UI
{
    public partial class FunctionManagement : BasePage
    {
        #region Properties
        int FunctionID
        {
            get
            {
                if (ViewState["FunctionID"] == null) return 0;
                return (int)ViewState["FunctionID"];
            }
            set
            {
                ViewState["FunctionID"] = value;
            }
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!AllowEditFunction())
            {
                RedirectToPage(AccessDeniedURL);
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindDataGrid();
            }
        }

        private void BindDataGrid()
        {
            rptObject.DataSource = DB.pepsi_Functions.OrderBy(p => p.OrderID);
            rptObject.DataBind();
        }

        private void BindDataRole(int functionID)
        {
            rptRole.DataSource =
                DB.pepsi_Roles.Select(p => new
                {
                    p.RoleId,
                    p.RoleName,
                    Checked = DB.pepsi_Function_Roles.Any(q => q.RoleID == p.RoleId && q.FunctionID == functionID)
                });
            rptRole.DataBind();
        }

        protected void rptObject_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Edit":
                    if (!AllowEditFunction())
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('You dont't have permission!')", true);
                        return;
                    }
                    FunctionID = Convert.ToInt32(e.CommandArgument);
                    BindDataRole(FunctionID);
                    ClientScript.RegisterStartupScript(this.GetType(), "", "dialog()", true);
                    break;
                default:
                    break;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var function_role = DB.pepsi_Function_Roles.Where(p => p.FunctionID == FunctionID);
            if (function_role != null)
                DB.pepsi_Function_Roles.DeleteAllOnSubmit(function_role);
            foreach (RepeaterItem item in rptRole.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("chkCheck");
                if (chk.Checked)
                {
                    HiddenField hdf = (HiddenField)item.FindControl("hdfRole");
                    pepsi_Function_Role newObj = new pepsi_Function_Role()
                    {
                        FunctionID = FunctionID,
                        RoleID = int.Parse(hdf.Value)
                    };
                    DB.pepsi_Function_Roles.InsertOnSubmit(newObj);
                }
            }
            DB.SubmitChanges();
        }
    }
}